// pages/bonusDetails/index.js
const {
  post,
  dateFormat
} = getApp();
Page({
  data: {
    option: [{
        text: '全部',
        value: ""
      },
      {
        text: '团费奖励金额',
        value: 2
      },
      {
        text: '佣金奖励金额',
        value: 1
      },
    ],
    data: {
      total_amount: 0,
      remain_amount: 0,
      pending_amount: 0,
    },
    params: {
      start_time: "",
      end_time: "",
      bounty_type: "",
      page: 1,
      pageSize: 20
    },
    dataList: [],
    showLoading: true,
    show: false,
    showPage: false,
    minDate: new Date(2020, 0, 1).getTime(),
    defaultDate: [new Date().getTime(), new Date(new Date().setDate(new Date().getDate() + 1)).getTime()]
  },
  // 初始化加载
  onLoad: function () {
    this.getList();
  },
  // 选项卡
  changeOptions(e) {
    this.setData({
      "params.bounty_type": e.detail,
      "params.page": 1,
      "dataList": [],
      "showLoading": true
    });
    this.getList();
  },
  // 获取数据
  getList() {
    if (this.data.showLoading) {
      post('/bounty/detail', this.data.params).then(res => {
        if (res.result == 1) {
          let data = this.data.dataList.concat(res.data.bountyOrderDetails);
          this.setData({
            data: {
              total_amount: res.data.total_amount,
              remain_amount: res.data.remain_amount,
              pending_amount: res.data.pending_amount,
            },
            showPage: true
          });
          if (res.data.bountyOrderDetails.length < this.data.params.pageSize) {
            this.setData({
              dataList: data,
              showLoading: false
            })
          } else {
            this.setData({
              dataList: data
            })
          }
        }
      });
    }
  },
  // 上拉加载
  bindscrolltolower: function () {
    this.setData({
      "params.page": this.data.params.page += 1
    });
    this.getList();
  },
  // 确定选择
  timeSelection() {
    this.selectComponent('#item').toggle();
    this.setData({
      "params.page": 1,
      "dataList": [],
      "showLoading": true
    });
    this.getList();
  },
  // 关闭日历
  onClose() {
    this.setData({
      show: false
    });
  },
  // 日期打开
  onDisplay() {
    this.setData({
      show: true
    });
  },
  // 日期选择
  onConfirm(e) {
    this.setData({
      "params.start_time": dateFormat(e.detail[0], "YYYY-MM-DD"),
      "params.end_time": dateFormat(e.detail[1], "YYYY-MM-DD"),
      "show": false,
      "showLoading": true
    });
  },
  onShow: function () {
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    });
  }
})